
import { Form } from 'choerodon-ui/pro';
import Record from 'choerodon-ui/pro/lib/data-set/Record';
import { FieldType } from 'choerodon-ui/pro/lib/data-set/enum';

const defaultValidationMessages = {
  valueMissing: '请输入{label}。（自定义）',
};

async function passwordValidator(value: string, _name: string | undefined, record: Record | undefined | Form): Promise<string | boolean | undefined> {

  if (record instanceof Record) {
    if (value !== record.get('password')) {
      return '您两次输入的密码不一致，请重新输入';
    }
  }
  return true;
}

const fromDs = () => ({
  autoCreate: true,
  fields: [
    {
      name: 'phone',
      type: FieldType.string,
      label: '手机号',
      labelWidth: '150',
      required: true,
      pattern: '^1[3-9]\\d{9}$',
    },
    {
      name: 'password',
      type: FieldType.string,
      label: '密码',
      required: true,
      defaultValidationMessages,
    },
    {
      name: 'confirmPassword',
      type: FieldType.string,
      label: '确认密码',
      required: true,
      validator: passwordValidator,
    },
    {
      name: 'age',
      type: FieldType.number,
      label: '年龄',
      required: true,
      min: 18,
      step: 1,
      help: '我们需要确定你的年龄',
    },
    { name: 'sex', type: FieldType.string, label: '性别', required: true },
    {
      name: 'language',
      type: FieldType.string,
      label: '语言',
      required: true,
      help: '超过两行的帮助信息超过两行的帮助信息超过两行的帮助信息',
    },
    {
      name: 'description',
      type: FieldType.string,
      label: '简介',
      required: true,
      help: '请输入100~300个字符请输入100~300个字符请输入100~300个字符',
    },
    { name: 'email', type: FieldType.email, label: '邮箱', required: true },
    { name: 'homepage', type: FieldType.url, label: '个人主页', required: true },
    { name: 'birth', type: FieldType.date, label: '生日', required: true },
    {
      name: 'code', type: FieldType.object, label: '代码描述', lovCode: 'LOV_CODE', lovDefineUrl: '/_api/sys/lov/lov_define',
      lovQueryUrl: '/_api/common/lov/dataset/LOV_CODE',
    },
    { name: 'frozen', type: FieldType.boolean, label: '是否冻结' },
  ],
});

export default fromDs;